package com.gs.SynPatrolRep.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.gs.SynPatrolRep.R;


public class InsightView extends FrameLayout {

    private TextView tvNum;
    private TextViewDC tvStart;

    public InsightView(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public InsightView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public InsightView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public InsightView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InsightView);
        String title = ta.getString(R.styleable.InsightView_item_insight_title);
        String btnText = ta.getString(R.styleable.InsightView_item_btn_text);

        View view = LayoutInflater.from(context).inflate(R.layout.rvitem_insight, this, false);
        TextView tvTitle = view.findViewById(R.id.tv_top);
        tvTitle.setText(title);
        tvNum = view.findViewById(R.id.tv_num);
        tvStart = view.findViewById(R.id.tv_start);
        if (TextUtils.isEmpty(btnText)) {
            tvStart.setVisibility(GONE);
        }else {
            tvStart.setText(btnText);
        }
        addView(view);
        ta.recycle();
    }

    public void setNum(int num){
        tvNum.setText(getContext().getString(R.string.zhang,num));
    }

    public void setOnClickListener(OnClickListener onClickListener){
        tvStart.setOnClickListener(onClickListener);
    }
}
